/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public abstract class Term implements INode {

    private enum MySubclass { Var, LocusVar, FreeVar, LambdaVar, Const, InfConst, Num, Func, PrivFunc, Fraenkel, Choice, QuaTrm, It, ErrorTrm,}

    public static boolean isTerm(String className){
        boolean found = false;
        for (MySubclass mySubclass :MySubclass.values()){
            if (mySubclass.name().equals(className)){
                found = true;
                break;
            }
        }
        return found;
    }

    public static Term newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        XMLElement xmlElement = xmlElementList.peek();
        if (!isTerm(xmlElement.getName())){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of Term", xmlElementList);
        }
        MySubclass mySubclass = MySubclass.valueOf(xmlElement.getName());
        switch (mySubclass) {
        case Var: return Var.newInstance(parentNode, xmlElementList);
        case LocusVar: return LocusVar.newInstance(parentNode, xmlElementList);
        case FreeVar: return FreeVar.newInstance(parentNode, xmlElementList);
        case LambdaVar: return LambdaVar.newInstance(parentNode, xmlElementList);
        case Const: return Const.newInstance(parentNode, xmlElementList);
        case InfConst: return InfConst.newInstance(parentNode, xmlElementList);
        case Num: return Num.newInstance(parentNode, xmlElementList);
        case Func: return Func.newInstance(parentNode, xmlElementList);
        case PrivFunc: return PrivFunc.newInstance(parentNode, xmlElementList);
        case Fraenkel: return Fraenkel.newInstance(parentNode, xmlElementList);
        case Choice: return Choice.newInstance(parentNode, xmlElementList);
        case QuaTrm: return QuaTrm.newInstance(parentNode, xmlElementList);
        case It: return It.newInstance(parentNode, xmlElementList);
        case ErrorTrm: return ErrorTrm.newInstance(parentNode, xmlElementList);
        default: throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of Term", xmlElementList);
        }
    }

    public LinkedList<XMLElement> getXMLElementList() {
        if (this instanceof Var) {
            return ((Var) this).getXMLElementList();
        } else 
        if (this instanceof LocusVar) {
            return ((LocusVar) this).getXMLElementList();
        } else 
        if (this instanceof FreeVar) {
            return ((FreeVar) this).getXMLElementList();
        } else 
        if (this instanceof LambdaVar) {
            return ((LambdaVar) this).getXMLElementList();
        } else 
        if (this instanceof Const) {
            return ((Const) this).getXMLElementList();
        } else 
        if (this instanceof InfConst) {
            return ((InfConst) this).getXMLElementList();
        } else 
        if (this instanceof Num) {
            return ((Num) this).getXMLElementList();
        } else 
        if (this instanceof Func) {
            return ((Func) this).getXMLElementList();
        } else 
        if (this instanceof PrivFunc) {
            return ((PrivFunc) this).getXMLElementList();
        } else 
        if (this instanceof Fraenkel) {
            return ((Fraenkel) this).getXMLElementList();
        } else 
        if (this instanceof Choice) {
            return ((Choice) this).getXMLElementList();
        } else 
        if (this instanceof QuaTrm) {
            return ((QuaTrm) this).getXMLElementList();
        } else 
        if (this instanceof It) {
            return ((It) this).getXMLElementList();
        } else 
        if (this instanceof ErrorTrm) {
            return ((ErrorTrm) this).getXMLElementList();
        } else 
        return new LinkedList<XMLElement>();
    }
        
}
